package top.houry.springbootkafka.consumer;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Properties;

public class ConsumerTest {
    public static void main(String[] args) {
        Properties properties = new Properties();

        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "182.61.19.148:9092");
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, "my-consumer-group-id");

        KafkaConsumer<String, String> records = new KafkaConsumer<>(properties);

        // records.subscribe(Arrays.asList("quickstart-events"));

        ArrayList<TopicPartition> partitions = new ArrayList<>();
        partitions.add(new TopicPartition("quickstart-events", 0));
        records.assign(partitions);

        while (true) {
            ConsumerRecords<String, String> consumerRecords = records.poll(Duration.ofSeconds(10));
            System.out.println("===========================================");
            System.out.println("===========================================");
            System.out.println("===========================================");
            for (ConsumerRecord<String, String> consumerRecord : consumerRecords) {
                System.out.println(consumerRecord);
            }
        }

    }
}
